package GUI;







import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Fachlogik.ExceptionWebShop;
import Fachlogik.Produkt;
import Fachlogik.WebShop;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import GUI.ProduktEditor;
import javax.swing.JTextField;
import javax.swing.JTabbedPane;
import javax.swing.JMenuItem;











public class Fenster extends JFrame
{
	
	
	
	private static final long serialVersionUID = 1L;

	private WebShop webshop;

	private JPanel contentPane;
	private JList<Produkt> produktListe;
	private ProduktEditor produkteditor;
	

	private DefaultListModel<Produkt> listModel = new DefaultListModel<>();
	private JTextField textSuche;
	private JMenuItem mntmNewMenuItem;
	
	

	
	

	
	public Fenster(WebShop webshop)
	{
		this.webshop = webshop;
		initFrame();
		
		produkteditor.setEnabled(false);  
		try
		{
			updateList();   
		} catch (ExceptionWebShop e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	
	public void updateList() throws ExceptionWebShop
	{
		listModel.clear();
		for (Produkt p : webshop.suchen(textSuche.getText()))
			listModel.addElement(p);
	}
	
	
	public void initFrame()
	{
		setTitle("WebShop");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(10, 10, 800, 500);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setToolTipText("WEBSHOP");
		setJMenuBar(menuBar);
		JMenu mnProdukt = new JMenu("Produkt");
		menuBar.add(mnProdukt);
		
		 JMenuItem mntmLschen= new  JMenuItem("L\u00F6schen");
		mntmLschen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				do_mntmLschen_actionPerformed(arg0);
			}
		});
		mnProdukt.add(mntmLschen);
		
		
		
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JSplitPane splitPane = new JSplitPane();
		contentPane.add(splitPane, BorderLayout.NORTH);
		
		
		
		
		
		JPanel left = new JPanel(new BorderLayout());
		splitPane.setLeftComponent(left);
		
		JPanel panelSuche = new JPanel();
		left.add(panelSuche);
		
		
		
		
		JScrollPane scrollPane = new JScrollPane();
		left.add(scrollPane, BorderLayout.CENTER);
		
		
		produktListe = new JList<Produkt>(listModel);
		produktListe.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				do_produktListe_valueChanged(arg0);
			}
		});
		scrollPane.setViewportView(produktListe);
		
		

		JPanel panel_3= new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Suche", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		left.add(panel_3, BorderLayout.NORTH);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		textSuche = new JTextField();
		textSuche.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				do_textSuche_keyReleased(arg0);
			}
		});

		panel_3.add(textSuche);
		textSuche.setColumns(10);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
	     splitPane.setRightComponent(tabbedPane);
		
	    
			
	        JPanel panel = new JPanel();
			tabbedPane.addTab("Produkt anelgen", null, panel, null);
			panel.setLayout(new BorderLayout(0, 0));
			
			produkteditor = new ProduktEditor();
			JScrollPane scrollPane_1 = new JScrollPane(produkteditor);
			panel.add(scrollPane_1, BorderLayout.CENTER);
			

			
			
			JPanel panel_2 = new JPanel();
			panel.add(panel_2, BorderLayout.SOUTH);
			
			
			
			JButton btnSpeichern = new JButton("Speichern");
			btnSpeichern.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					do_btnSpeichern_actionPerformed1(e);
				}
			});
			panel_2.add(btnSpeichern);
	
			
			JButton btnNewButton = new JButton("Neu");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					do_btnNewButton_actionPerformed(e);
				}
			});
			
	panel_2.add(btnNewButton);	
	

	
	}
	
	
	protected void do_btnSpeichern_actionPerformed1(ActionEvent e) 
	{
		try
		{
		Produkt p = produkteditor.getProdukt();  
			updateList();
			produktListe.setSelectedValue(p, true);  
			
		} catch (ExceptionWebShop e1 )
		{
			JOptionPane.showMessageDialog(this, e1.getMessage());
		}
		
	}
	
	

	
	
	protected void do_produktListe_valueChanged(ListSelectionEvent arg0) 
	{
		
		produkteditor.setEnabled(produktListe.getSelectedValue() != null);
		try
		{
			
			produkteditor.getProdukt();  
			Produkt p = produktListe.getSelectedValue();
			produkteditor.setProdukt(p);
		} catch (ExceptionWebShop e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	
	
	 
	
	protected void do_tfFilter_keyReleased(KeyEvent arg0) 
	{
		try
		{
			updateList();
		} catch (ExceptionWebShop e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	protected void do_textSuche_keyReleased(KeyEvent arg0) 
	{
		try
		{
			updateList();
		} catch (ExceptionWebShop e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage());
		}
	}
	
	

	protected void do_mntmLschen_actionPerformed(ActionEvent arg0) {
		
		

		Produkt p = produktListe.getSelectedValue();
		if (p != null)
		{
			try
			{
				webshop.entfernen(p);
				updateList();
			} catch (ExceptionWebShop e)
			{
				e.printStackTrace();
				JOptionPane.showMessageDialog(this, e.getMessage());
			}
		}
	}
	protected void do_btnNewButton_actionPerformed(ActionEvent e) {
		

		try
		{
			Produkt p = new Produkt(null);
			webshop.neu(p);
			updateList();
			produktListe.setSelectedValue(p, true);
		} catch (ExceptionWebShop e1)
		{
			e1.printStackTrace();
			JOptionPane.showMessageDialog(this, e1.getMessage());
		}
	}
}

